import 'package:flutter/material.dart';
import 'package:flutter_gallery/demos/material/material_demo_types.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';

class BottomSheetDemo extends StatelessWidget {
  const BottomSheetDemo({Key? key, required this.type}) : super(key: key);

  final BottomSheetDemoType type;

  String _title(BuildContext context) {
    switch (type) {
      case BottomSheetDemoType.persistent:
        return GalleryLocalizations.of(context)!.demoBottomSheetPersistentTitle;
      case BottomSheetDemoType.modal:
        return GalleryLocalizations.of(context)!.demoBottomSheetModalTitle;
    }
  }

  Widget _bottomSheetDemo(BuildContext context) {
    switch (type) {
      case BottomSheetDemoType.persistent:
        return const _PersistentBottomSheetDemo();
      case BottomSheetDemoType.modal:
      default:
        return const _ModalBottomSheetDemo();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Navigator(
      // Adding [ValueKey] to make sure that the widget gets rebuilt when
      // changing type.
      key: ValueKey(type),
      onGenerateRoute: (setting) {
        return MaterialPageRoute<void>(
          builder: (context) => Scaffold(
            appBar: AppBar(
              automaticallyImplyLeading: false,
              title: Text(_title(context)),
            ),
            floatingActionButton: FloatingActionButton(
              onPressed: () {
                Fluttertoast.showToast(msg: 'msg');
              },
              backgroundColor: Theme.of(context).colorScheme.secondary,
              child: Icon(
                Icons.add,
                semanticLabel:
                    GalleryLocalizations.of(context)!.demoBottomSheetAddLabel,
              ),
            ),
            body: _bottomSheetDemo(context),
          ),
        );
      },
    );
  }
}

// BEGIN bottomSheetContent
class _BottomSheetContent extends StatelessWidget {
  const _BottomSheetContent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        height: 300,
        child: Column(
          children: [
            SizedBox(
              height: 70,
              child: Center(
                child: Text(
                  GalleryLocalizations.of(context)!.demoBottomSheetHeader,
                  textAlign: TextAlign.center,
                ),
              ),
            ),
            const Divider(thickness: 1),
            Expanded(
                child: ListView.builder(
              itemCount: 21,
              itemBuilder: (context, index) => ListTile(
                title: Text(
                  GalleryLocalizations.of(context)!.demoBottomSheetItem(index),
                ),
              ),
            ))
          ],
        ));
  }
}

// BEGIN bottomSheetDemoPersistent
class _PersistentBottomSheetDemo extends StatefulWidget {
  const _PersistentBottomSheetDemo({Key? key}) : super(key: key);

  @override
  _PersistentBottomSheetDemoState createState() =>
      _PersistentBottomSheetDemoState();
}

class _PersistentBottomSheetDemoState
    extends State<_PersistentBottomSheetDemo> {
  VoidCallback? _showBottomSheetCallback;

  @override
  void initState() {
    super.initState();
    _showBottomSheetCallback = _showPersistentBottomSheet;
  }

  void _showPersistentBottomSheet() {
    setState(() {
      // Disable the show bottom sheet button.
      _showBottomSheetCallback = null;
    });

    Scaffold.of(context)
        .showBottomSheet<void>(
          (context) => const _BottomSheetContent(),
          elevation: 25,
        )
        .closed
        .whenComplete(() {
      if (mounted) {
        setState(() {
          // Re-enable the bottom sheet button.
          _showBottomSheetCallback = _showPersistentBottomSheet;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
          onPressed: _showBottomSheetCallback,
          child: Text(
              GalleryLocalizations.of(context)!.demoBottomSheetButtonText)),
    );
  }
}

// BEGIN bottomSheetDemoModal
class _ModalBottomSheetDemo extends StatelessWidget {
  const _ModalBottomSheetDemo({Key? key}) : super(key: key);

  void _showModalBottomSheet(BuildContext context) {
    showModalBottomSheet(
        context: context, builder: (context) => const _BottomSheetContent());
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () {
          _showModalBottomSheet(context);
        },
        child: Text(
          GalleryLocalizations.of(context)!.demoBottomSheetButtonText,
        ),
      ),
    );
  }
}
